public class Solution4 {
    //132. 分割回文串 II
    public int minCut(String s) {
        int n = s.length();
        boolean[][] isPel = new boolean[n][n];
        for (int i = n-1; i >= 0; i--) {
            for (int j = i; j < n; j++) {
                if (s.charAt(i) == s.charAt(j)) {
                    if (i == j) {
                        isPel[i][j] = true;
                    } else if (i+1 == j) {
                        isPel[i][j] = true;
                    } else {
                        isPel[i][j] = isPel[i+1][j-1];
                    }
                }
            }
        }
        int[] dp = new int[n]; //以i结尾的最少分割次数
        for (int i = 0; i < n; i++) {
            dp[i] = Integer.MAX_VALUE;
        }
        for (int i = 0; i < n; i++) {
            if (isPel[0][i]) {
                dp[i] = 0;
            } else {
                for (int j = 1; j <= i; j++) {
                    if (isPel[j][i]) {
                        dp[i] =  Math.min(dp[i],dp[j-1] + 1);
                    }
                }
            }
        }
        return dp[n-1];
    }
}
